import {mainNetWork} from "./env"
import { ethers, providers, utils } from "ethers";
export async function filterEvent() {
    const main = async () => {
       // 1. 创建 provide 实例
       const provide = new ethers.providers.JsonRpcProvider(mainNetWork)
       // 2. 创建合约实例
       const usdtAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7'
       const abi = [
         "event Transfer(address indexed from, address indexed to, uint value)",
         "function balanceOf(address) public view returns(uint)"
       ];
      const usdtContract = new ethers.Contract(usdtAddress,abi,provide)
      // 交易所地址
      const accountBinance = '0x28C6c06298d514Db089934071355E5743bf21d60'
      const balanceUSDT = await usdtContract.balanceOf(accountBinance)
      console.log(`币安的USDT余额 ${ethers.utils.formatUnits(ethers.BigNumber.from(balanceUSDT), 6)}`)
      // 创建过滤器 监听币安安的 USDT 增加的事件
      const filterBinanceIn = usdtContract.filters.Transfer(null , accountBinance)
      usdtContract.on(filterBinanceIn, (from, to ,value) => {
        console.log('---------监听USDT进入交易所--------');
        console.log(`从${from}地址 => 转入币安${to} ,${ethers.utils.formatUnits(ethers.BigNumber.from(value), 6)}USDT`)
      })
    }
    main()
}